package com.kaoshi.myweb.web.support.tld;

import java.util.Collection;
import java.util.Iterator;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.TagSupport;

public class RepeaterTag extends TagSupport {
	private Collection items;
	private Iterator iterator;
	private String var;
	
	
	@Override
	public int doStartTag() throws JspException {
		if(items == null || items.size() ==0 )
			return SKIP_BODY;
		
		iterator = items.iterator();
		
		if(iterator.hasNext())
			pageContext.setAttribute(var, iterator.next());
		
		return EVAL_BODY_INCLUDE;
	}
	
	@Override
	public int doAfterBody() throws JspException {
		if(iterator.hasNext()){
			pageContext.setAttribute(var, iterator.next());
			return EVAL_BODY_AGAIN;
		}
		
		return SKIP_BODY;
	}
	
	@Override
	public int doEndTag() throws JspException {
		return EVAL_PAGE;
	}
	
	
	public Collection getItems() {
		return items;
	}
	public void setItems(Collection items) {
		this.items = items;
	}
	public Iterator getIterator() {
		return iterator;
	}
	public void setIterator(Iterator iterator) {
		this.iterator = iterator;
	}
	public String getVar() {
		return var;
	}
	public void setVar(String var) {
		this.var = var;
	}
	
	
	
}
